import lodsah from 'lodash';
import fetch from 'node-fetch';

const ActionHelper = {
  // 服务端action
  recharge: '充值',
  withdraw: '提现',
  create_group: '新建群组',
  group_red_packet: '群红包',
  group_gathering_money: '群收款',
};

let request = null;

const StatisticsHelper = function(options) {
  const { request = fetch, endpoint } = options;
  if (!(request && endpoint)) {
    console.error('启动任务失败，统计地址与统计post', request, endpoint);
  }

  const post = function(body) {
    return request(endpoint, {
      method: 'post',
      body: JSON.stringify(body),
      headers: {
        'content-type': 'application/json',
      },
    }).then(response => response.json());
  };

  return {
    recharge: ({ money, userId }) => {
      const action = ActionHelper.recharge;
      const body = { action, money, userId };
      return post(body);
    },

    withdraw: ({ money, userId }) => {
      const action = ActionHelper.withdraw;
      const body = { action, money, userId };
      return post(body);
    },

    create_group: ({ userId, groupId, groupName }) => {
      const action = ActionHelper.create_group;
      const body = { action, money: 0, userId, groupId };
      return post(body);
    },

    group_red_packet: ({ userId, groupId, money, groupName, orderId }) => {
      const action = ActionHelper.group_red_packet;
      const body = { action, money, userId, groupId, groupName, orderId };
      return post(body);
    },

    group_gathering_money: ({ userId, groupId, money, groupName, orderId }) => {
      const action = ActionHelper.group_gathering_money;
      const body = { action, money, userId, groupId, groupName, orderId };
      return post(body);
    },
  };
};

export default StatisticsHelper;
